package cn.lf.android.lawfirm;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.lf.android.lawfirm.utils.KeyBoardUtils;

public abstract class OrdinaryBaseActivity extends BaseActivity {
    protected RelativeLayout mRL_search;
    protected TextView tvSearch;
    // 标题栏
    /**
     * 左侧按钮
     **/
    protected ImageButton mImgLeft;
    /**
     * 右侧按钮
     **/
    protected ImageButton mImgRight;
    /**
     * 中间按钮
     **/
    protected ImageButton mImgCenter;
    /**
     * 标题
     **/
    protected TextView mTitle;
    /**
     * 右侧文字
     **/
    protected TextView mTvRight;
    /**
     * 左侧文字
     **/
    protected TextView mTvLeft;
    /**
     * 搜索内容
     **/
    public EditText mSearch;
    /**
     * 清除搜索框内容
     **/
    protected ImageView mClean;
    /**
     * 取消
     **/
    protected ImageView mSelectBtn;
    /**
     * 显示隐藏搜索框布局
     **/
    protected RelativeLayout mSearchLayout;
    /**
     * tatlebar的蒙版
     **/
    protected View mengban_bar;
    // 网络异常 无数据 加载失败
    /**
     * 无数据提示信息 显示内容自行settext()
     **/
    protected TextView noData,noData2;
    private LinearLayout unNetwork;
    protected FrameLayout frameLayout;
    private View childView;
    protected RelativeLayout mTitleBar;

    /**
     * 返回布局最外层View
     */
    public abstract int getChildView();

    /**
     * 初始化控件
     * <p>
     * *******注意********* 要用findId(int id)初始化 不要用findViewById
     */
    public abstract void initViewId();

    /**
     * 重新加载数据
     * <p>
     * 必须重写该方法才能实现无网路及网络异常的正常显示及消失 在该方法内重新调用数据加载的方法即可
     */
    public abstract void refreshLoad();

    /**
     * 点击事件
     *
     * @param v
     */
    public abstract void widgetClick(View v);

    @Override
    public Object getContentResView() {
        return R.layout.activity_ordinarybase;
    }

    @Override
    public void initView() {

        // product_tab_id
        mTitleBar = (RelativeLayout) findViewById(R.id.titlebar_id);
        mImgLeft = (ImageButton) findViewById(R.id.img_left);
        mImgRight = (ImageButton) findViewById(R.id.img_right);
        mImgCenter = (ImageButton) findViewById(R.id.img_center);
        mTitle = (TextView) findViewById(R.id.eb_tv_title);
        mTvRight = (TextView) findViewById(R.id.eb_tv_right);
        mTvLeft = (TextView) findViewById(R.id.eb_tv_left);
        mClean = (ImageView) findViewById(R.id.imgClean);
        mSelectBtn = (ImageView) findViewById(R.id.select);
        mSearch = (EditText) findViewById(R.id.edtSearch);
        mSearchLayout = (RelativeLayout) findViewById(R.id.layoutSearch);
        mengban_bar = (View) findViewById(R.id.mengban_bar);
        mClean.setOnClickListener(this);
        unNetwork = (LinearLayout) findViewById(R.id.unNetwork);
        mRL_search = (RelativeLayout) findViewById(R.id.rl_search);
        mRL_search.setOnClickListener(this);
        noData = (TextView) findViewById(R.id.noData);
        noData2= (TextView) findViewById(R.id.noData2);
        tvSearch= (TextView) findViewById(R.id.tvSearch);
        unNetwork.setOnClickListener(this);
        if (!isNetWorkConnected()) {
            showUnNetWork();
        }
        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        childView = LayoutInflater.from(mActivity)
                .inflate(getChildView(), null);

        initViewId();

        frameLayout.addView(childView);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {// 无网络点击事件处理
            case R.id.unNetwork:
                if (!isNetWorkConnected()) {
                    return;
                } else {
                    showNormal();
                }
                refreshLoad();
                break;
            case R.id.imgClean: // 加载失败点击事件处理
                if (isClear) {
                    mSearch.setText("");
                }
                break;

        }

        widgetClick(v);
    }

    private boolean isClear = true;

    /**
     * 是否执行父类的清除方法
     *
     * @param isClear
     */
    public void isClearText(boolean isClear) {
        this.isClear = isClear;
    }

    /**
     * 显示无网络
     */
    protected void showUnNetWork() {

        if (unNetwork.getVisibility() == View.GONE) {
            unNetwork.setVisibility(View.VISIBLE);
        }
        if (noData.getVisibility() == View.VISIBLE) {
            noData.setVisibility(View.GONE);
        }
    }

    /**
     * 显示无数据
     */
    protected void showNoData() {

        if (unNetwork.getVisibility() == View.VISIBLE) {
            unNetwork.setVisibility(View.GONE);
        }
        if (noData.getVisibility() == View.GONE) {
            noData.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加载异常 在网络请求后的数据异常的情况下 如500等 页面不存在其他数据时用该方法，如页面之前存在数据调用toastUnKnown()
     */
    public void showLoadFail() {

        if (unNetwork.getVisibility() == View.VISIBLE) {
            unNetwork.setVisibility(View.GONE);
        }
        if (noData.getVisibility() == View.VISIBLE) {
            noData.setVisibility(View.GONE);
        }
    }

    /**
     * 显示正常
     */
    public void showNormal() {

        if (unNetwork.getVisibility() == View.VISIBLE) {
            unNetwork.setVisibility(View.GONE);
        }
        if (noData.getVisibility() == View.VISIBLE) {
            noData.setVisibility(View.GONE);
        }

    }

    /**
     * 子类需要用该方法初始化控件
     *
     * @param id
     * @return
     */
    protected View findId(int id) {
        return childView.findViewById(id);
    }

    public void onClearEdit() {
        mSearch.clearFocus();
        KeyBoardUtils.closeKeybord(mSearch, mActivity);
    }

    // 隐藏输入键盘
    public void hintKbTwo() {
        KeyBoardUtils.closeKeybord(mSearch, this);
    }
    //打开输入键盘
    public void openKeybord() {
        KeyBoardUtils.openKeybord(mSearch, this);
    }
}
